package org.abettor.tanqiu.gameloop;

import org.abettor.tanqiu.gameloop.object.BallMap;
import org.abettor.tanqiu.gameloop.object.BallMapSpherePart;
import org.abettor.tanqiu.gameloop.object.BallMapSquarePart;
import org.abettor.tanqiu.screen.Screen;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector2;


public class GameLoop implements Screen,SimulationListener{
	private Simulation simulation;
	private SimulationRenderer renderer;
	/**
	 * 构造函数
	 * @param app
	 */
	public GameLoop(Application app){
		simulation = new Simulation();
		renderer = new SimulationRenderer();
		BallMap map = createBallMap();
		simulation.setMap(map);
	}
	@Override
	public void update(Application app) {
		simulation.update(app.getGraphics().getDeltaTime());
	}

	@Override
	public void present(Application app) {
		app.getGraphics().getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		renderer.render(app, simulation);//进行渲染的操作
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		renderer.dispose();
	}
	//创建地图
	private BallMap createBallMap(){
		BallMapSquarePart part1 = new BallMapSquarePart(BallMapSquarePart.TYPE_HORIZONTAL,new Vector2(0,10),new Vector2(160,10));
		BallMapSpherePart part2 = new BallMapSpherePart(BallMapSpherePart.TYPE_6,new Vector2(160,10+Constants.SPHEREPART_RAIDUS-Constants.BALL_RAIDUS),BallMapSpherePart.POT_TYPE_START);
		BallMapSpherePart part3 = new BallMapSpherePart(BallMapSpherePart.TYPE_5,new Vector2(160,10+Constants.SPHEREPART_RAIDUS-Constants.BALL_RAIDUS),BallMapSpherePart.POT_TYPE_END);
		BallMapSquarePart part4 = new BallMapSquarePart(BallMapSquarePart.TYPE_VERTICAL,new Vector2(160+Constants.SPHEREPART_RAIDUS-Constants.BALL_RAIDUS,10+Constants.SPHEREPART_RAIDUS-Constants.BALL_RAIDUS),
				new Vector2(160+Constants.SPHEREPART_RAIDUS-Constants.BALL_RAIDUS,150));
		BallMap map = new BallMap(part1,part2,part3,part4);
		return map;
	}
}
